package cn.com.chengmandian.bike.controller;


import cn.com.chengmandian.agent.utils.AuthUtil;
import cn.com.chengmandian.bike.dto.MessageInfoDto;
import cn.com.chengmandian.bike.dto.ReadMessageForm;
import cn.com.chengmandian.bike.log.ActionLog;
import cn.com.chengmandian.bike.model.EbMessageInfo;
import cn.com.chengmandian.bike.model.vo.SysMessageTotalVo;
import cn.com.chengmandian.bike.service.IEbMessageInfoService;
import cn.com.chengmandian.core.common.constants.CommonConstant;
import cn.com.chengmandian.core.db.pojo.PageData;
import cn.com.chengmandian.core.web.pojo.AgentUser;
import cn.com.chengmandian.core.web.pojo.AjaxResult;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 电单车运营-用户消息 前端控制器
 * </p>
 *
 * @author AutoGenerator
 * @since 2021-12-30
 */
@Api(tags = "用户消息接口")
@RestController
@RequestMapping("/message/info")
public class EbMessageInfoController {

    @Resource
    private IEbMessageInfoService messageInfoService;

    @ApiOperation("用户消息分页列表")
    @GetMapping("/page")
    public AjaxResult<?> selectMessageChannelList(MessageInfoDto messageInfoDto) {
        Assert.isTrue(messageInfoDto.getChannelId() != null,"频道id不能为空");
        AgentUser user = AuthUtil.getAgentUser();
        messageInfoDto.setMemberId(user.getId());
        return AjaxResult.success(PageData.build(messageInfoService.getPage(messageInfoDto)));
    }

    @ApiOperation("编辑用户消息读取状态")
    @PostMapping("/edit/{id}/{isRead}")
    @ActionLog(name="编辑用户消息读取状态",description = "'参数id：' + #id&'isRead：'+#isRead")
    public AjaxResult<?> edit(@PathVariable("id") Integer id, @PathVariable("isRead") Integer isRead) {
        Assert.isTrue(id != null,"消息id不能为空");
        Assert.isTrue(isRead != null,"已读状态不能为空");
        AgentUser user = AuthUtil.getAgentUser();
        messageInfoService.update(new UpdateWrapper<EbMessageInfo>().lambda()
                .set(EbMessageInfo::getIsRead,isRead)
                .eq(EbMessageInfo::getId,id)
                .eq(EbMessageInfo::getMemberId,user.getId()));
        return AjaxResult.success("编辑成功");
    }

    @ApiOperation("一键阅读")
    @PostMapping("/channel/read")
    public AjaxResult readMessage(@RequestBody ReadMessageForm form) {
        Assert.notNull(form.getChannelId(), "缺少频道");

        messageInfoService.update(new UpdateWrapper<EbMessageInfo>().lambda()
                .set(EbMessageInfo::getIsRead,CommonConstant.ONE)
                .eq(EbMessageInfo::getChannelId,form.getChannelId())
                .eq(EbMessageInfo::getMemberId,AuthUtil.getAgentUser().getId()));
        return AjaxResult.success();
    }

    @ApiOperation("消息统计")
    @GetMapping("/total")
    public AjaxResult<SysMessageTotalVo> total() {
        return AjaxResult.success(messageInfoService.total(AuthUtil.getAgentUser().getId()));
    }
}
